deriving the law of cosines